<?php


namespace app\console\controller;


use app\BaseController;
use app\console\service\category\CategoryQueryService;
use app\console\service\category\CategoryService;
use app\console\validate\CategoryValidate;
use app\lib\exception\ParamException;
use app\model\Category;
use app\Request;
use think\App;
use think\exception\ValidateException;

function category2dict($list)
{
    $dateTime = app('DateTime');

    if (empty($list['children']))
    {
        $list['children'] = [];
    }

    return [
        'id' => $list['id'],
        'name' => $list['name'],
        'number' => $list['number'],
        'type' => $list['type'],
        'tags' => json_decode($list['tags'],true),
        'parent_id' => $list['parent_id'],
        'level' => $list['level'],
        'ratio' => $list['ratio'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' => $dateTime->timestamp2date($list['create_time']),
        'format_update_time' => $dateTime->timestamp2date($list['update_time']),
        'children' => $list['children']
    ];
}

function categorylist2dict($data)
{
    $list = array();
    if ($data == [])
    {
        return $data;
    }

    foreach ($data as $value)
    {
        $value = category2dict($value);
        $value['children'] = categorylist2dict($value['children']);
        $list[] = $value;
    }

    return $list;
}
//分类
class CategoryController extends BaseController
{
    public function create(Request $request, CategoryService $categoryService)
    {
        $param = $request->param(['name', 'number', 'type', 'ratio']);
        $param['tags'] = $request->param('tags', array());
        $param['parent_id'] = $request->param('parent_id', 0);

        try
        {
            validate(CategoryValidate::class)
                ->scene('create')
                ->check($param);
        } catch (ValidateException $e) {
            throw new ParamException($e->getMessage());
        }

        $category = $categoryService->create($param);

        return $this->returnJson(category2dict($category));
    }

    public function get(Request $request, CategoryQueryService $categoryQueryService)
    {
        $page = $request->get('page', false);
        $all = $request->get('all', 1);
        $perPage = $request->get('perPage', config('app.allow_limit'));
        $type = $request->get('type', 0);

        if (!$all)
        {
            try
            {
                validate(CategoryValidate::class)
                    ->scene('getPage')
                    ->check([
                        'page'         => $page ,
                        'perPage'      => $perPage ,
                        'type'      => $type ,
                    ]);
            } catch (ValidateException $e) {
                throw new ParamException($e->getMessage());
            }

            $categories = $categoryQueryService->getPageListByType($type, $page, $perPage);
        }else{

            try
            {
                validate(CategoryValidate::class)
                    ->scene('get')
                    ->check([
                        'type'      => $type ,
                    ]);
            } catch (ValidateException $e) {
                throw new ParamException($e->getMessage());
            }

            $categories = $categoryQueryService->getListByType($type);
        }

        return $this->returnList(categorylist2dict($categories['list']),
            $categories['pagination']['page'], $categories['pagination']['perPage'], $categories['pagination']['total']
        );
    }

    public function update(Request $request, CategoryService $categoryService)
    {
        $param = $request->param(['name', 'ratio'], '');
        $param['tags'] = $request->param('tags', '');

        if (empty($param))
        {
            throw new ParamException('未修改任何数据');
        }

        $category = $categoryService->update($param);

        return $this->returnJson(category2dict($category));
    }

    public function delete(CategoryService $categoryService)
    {
        $categoryService->delete();
        return $this->returnJson();
    }

    public function getById(Category $category)
    {
        return $this->returnJson(category2dict($category));
    }
}